﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Sita.Data.Context;
using Sita.Entities;
using Sita.Data.Core;

namespace Sita.Data.Repositories
{
    public class ReplacementRepository : GenericRepository<Replacement>
    {
        public ReplacementRepository() :
            base(new SitaContext())
        { }

        public IList<Replacement> GetPaginatedByBusinessUnitID(int businessUnitID, int pageIndex, int pageSize, string sortColumn, bool ascending, out int totalRows)
        {
            RepositoryHelper<Replacement> Helper = new RepositoryHelper<Replacement>();
            var OrderByExpr = Helper.GetOrderExpression(sortColumn);

            IQueryable<Replacement> ReturnSet;


            ReturnSet = (base.StoreContext as ISitaContext).Replacements
                .Where(r => r.BusinessUnitID == businessUnitID)
                .Select(buu => buu);

            totalRows = ReturnSet.Count();

            return (ascending)
                                ?
                                    ((IOrderedQueryable<Replacement>)System.Linq.Queryable.OrderBy(ReturnSet, OrderByExpr))
                                        .Skip(pageIndex * pageSize)
                                        .Take(pageSize)
                                        .Include("ReplacementBrand")
                                        .ToList()
                                :
                                    ((IOrderedQueryable<Replacement>)System.Linq.Queryable.OrderByDescending(ReturnSet, OrderByExpr))
                                        .Skip(pageIndex * pageSize)
                                        .Take(pageSize)
                                        .Include("ReplacementBrand")
                                        .ToList();

        }

        public IList<Replacement> GetPaginatedByFilter(int businessUnitID, int pageIndex, int pageSize, string sortColumn, bool ascending, string ShelfLocation,
            string RowLocation, string ColumnLocation, string Name, string Code, int ReplacementBrandID, int StockFrom, int StockTo, int MinimumStock, decimal PublicPriceFrom,
            decimal PublicPriceTo, out int totalRows)
        {
            RepositoryHelper<Replacement> Helper = new RepositoryHelper<Replacement>();
            var OrderByExpr = Helper.GetOrderExpression(sortColumn);

            IQueryable<Replacement> ReturnSet;


            ReturnSet = (base.StoreContext as ISitaContext).Replacements
                .Where(r => r.BusinessUnitID == businessUnitID &&
                            (r.ShelfLocation.Contains(ShelfLocation) || string.IsNullOrEmpty(ShelfLocation)) &&
                            (r.RowLocation.Contains(RowLocation) || string.IsNullOrEmpty(RowLocation)) &&
                            (r.ColumnLocation.Contains(ColumnLocation) || string.IsNullOrEmpty(ColumnLocation)) &&
                            (r.Name.Contains(Name) || string.IsNullOrEmpty(Name)) &&
                            ((r.Code.Contains(Code) || r.CodeExtra1.Contains(Code) || r.CodeExtra2.Contains(Code)) || string.IsNullOrEmpty(Code)) &&
                            (r.ReplacementBrandID == ReplacementBrandID || ReplacementBrandID == 0) &&
                            ((StockFrom == 0 || r.Stock >= StockFrom) && (StockTo == 0 || r.Stock <= StockTo)) &&
                            (MinimumStock == 0 || r.MinimumStock == MinimumStock) &&
                            ((PublicPriceFrom == 0 || r.PublicPrice >= PublicPriceFrom) && (PublicPriceTo == 0 || r.PublicPrice <= PublicPriceTo)))
                .Select(buu => buu);

            totalRows = ReturnSet.Count();

            return (ascending)
                                ?
                                    ((IOrderedQueryable<Replacement>)System.Linq.Queryable.OrderBy(ReturnSet, OrderByExpr))
                                        .Skip(pageIndex * pageSize)
                                        .Take(pageSize)
                                        .Include("ReplacementBrand")
                                        .ToList()
                                :
                                    ((IOrderedQueryable<Replacement>)System.Linq.Queryable.OrderByDescending(ReturnSet, OrderByExpr))
                                        .Skip(pageIndex * pageSize)
                                        .Take(pageSize)
                                        .Include("ReplacementBrand")
                                        .ToList();

        }

        public Replacement GetByIDWithWO(int ReplacementID)
        {
            Replacement ReturnSet;
            ReturnSet = (base.StoreContext as ISitaContext).Replacements
                .Where(r => r.ReplacementID == ReplacementID)
                .Include("WorkOrderDetailReplacements")
                .SingleOrDefault();

            return ReturnSet;
        }

        public IList<Replacement> GetByBusinessUnitIDByFilter(int businessUnitID, string filter)
        {
            string sortColumn = "Name";

            RepositoryHelper<Replacement> Helper = new RepositoryHelper<Replacement>();
            var OrderByExpr = Helper.GetOrderExpression(sortColumn);

            IQueryable<Replacement> ReturnSet;

            ReturnSet = (base.StoreContext as ISitaContext).Replacements
                .Where(r => r.BusinessUnitID == businessUnitID && (r.Name.Contains(filter) || r.Code.Contains(filter) || r.CodeExtra1.Contains(filter) || r.CodeExtra2.Contains(filter)))
                .Select(r => r);


            return ((IOrderedQueryable<Replacement>)System.Linq.Queryable.OrderBy(ReturnSet, OrderByExpr))
                                        .ToList();
        }

        public IList<Replacement> GetLowStockReplacements(int businessUnitID)
        {
            IQueryable<Replacement> ReturnSet;
            ReturnSet = (base.StoreContext as ISitaContext).Replacements
                .Where(r => r.BusinessUnitID == businessUnitID && r.Stock < r.MinimumStock && r.Stock != 0)
                .Select(r => r);

            return ReturnSet.ToList();
        }

        public IList<Replacement> GetOutOfStockReplacements(int businessUnitID)
        {
            IQueryable<Replacement> ReturnSet;
            ReturnSet = (base.StoreContext as ISitaContext).Replacements
                .Where(r => r.BusinessUnitID == businessUnitID && r.Stock == 0)
                .Select(r => r);

            return ReturnSet.ToList();
        }
    }
}
